<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close" :append-to-body="true" :modal-append-to-body="true">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit">
      <el-row>
        <el-col :span="12">
          <el-form-item label="里程碑及任务名称" prop="milepostname" :rules="rules.validateRequire">
            <el-input v-model="model.milepostname"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="责任人" prop="responsible" :rules="rules.validateRequire">
            <el-input v-model="model.responsible" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openmpmProjectTeamRefDialog"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="计划达成日期" prop="planfinishtime" :rules="rules.validateRequire">
            <el-date-picker v-model="model.planfinishtime" type="date" placeholder="例：2021-01-01" value-format="yyyy-MM-dd" @change="selectTime"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="交付成果" prop="deliverable" :rules="rules.validateRequire">
            <el-input v-model="model.deliverable"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="disableEditSubmit === false">确认</el-button>
    </div>
    <tlmpm-project-team-ref ref="mpmProjectTeamRef" @confirm="getMpmProjectTeamData" :externalQueryParam="externalQueryParamForPool">
    </tlmpm-project-team-ref>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_edit } from '@tapui/mixins/mixin_edit'
import { formatDate } from '@tapui/utils/date'
import { validateNull } from '@tapui/utils/validate'
/** 子组件 */
import tlmpmProjectTeamRef from '@/views/mpm/mpmProjectTeam/refList'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: { tlmpmProjectTeamRef },
  data() {
    return {
      moduleTitle: '项目策划-里程碑及任务',
      urlPrefix: '/mpm/projectMilepost/',
      externalQueryParamForPool: {},
      url: {
        // 额外的url
        queryMaxTaskDate: '/mpm/projectMilepost/queryMaxTaskDate',
        checkPlanUnfinish: '/mpm/projectPlan/checkPlanUnfinish'
      },
      maxTaskDate: '',
      planUnfinish: false
    }
  },
  props: {
    parentId: {
      type: [Number, String],
      default: ''
    },
    operatecode: {
      type: String
    },
    projectinfo: {

    }
  },
  methods: {
    // userConfirm(selectData) {
    //   const selectedUser = selectData[0]
    //   const { code, name } = selectedUser
    //   this.$set(this.model, "responsiblecode", code)
    //   this.$set(this.model, "responsible", name)
    //   this.$set(this.model, "projectcode", this.projectinfo.projectcode)
    // },
    loaded() {
      // 页面加载完毕执行
      if (this.isAdd) {
        this.$set(this.model, 'planid', this.parentId)
      }
      // 编辑加载时，获取该里程碑对应任务信息
      if (!this.isAdd && !this.disableEditSubmit) {
        getAction(this.url.queryMaxTaskDate, { qry_milepostcode: this.model.operatecode }).then(res => {
            if (res.success) {
              this.maxTaskDate = res.result.maxdate
            }
        })
        this.checkPlanUnfinish()
      }
    },
    getMpmProjectTeamData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'responsiblecode', selectionRow.usercode)
      this.$set(this.model, 'responsible', selectionRow.username)
      this.$set(this.model, "projectcode", this.projectinfo.projectcode)
    },
    openmpmProjectTeamRefDialog() {
      this.externalQueryParamForPool = {
        qry_projectcode: this.projectinfo.projectcode,
        qry_state: 9,
        qry_enabled: 1,
        qry_appointmenttype: 1
      }
      this.$refs.mpmProjectTeamRef.visible = true
    },
    beforeSubmit() {
      if (this.planUnfinish) {
        this.$tap.alert('该项目存在未审批完成的策划书，请审批完成后再修改里程碑信息！')
        return false
      }
      this.$set(this.model, 'fromindex', true)
      this.$set(this.model, 'singlechangeuser', this.user.code)
      return true
    },
    // 判断里程碑的计划达成时间
    selectTime() {
      let time = this.model.planfinishtime
      if (this.formatDateToMicro(time) > this.formatDateToMicro(this.projectinfo.planenddate)) {
        this.$tap.alert('里程碑及任务的计划达成时间不能晚于所属项目的计划结束时间！')
        this.$set(this.model, 'planfinishtime', null)
        return
      }
      if (this.formatDateToMicro(time) < this.formatDateToMicro(this.projectinfo.planstartdate)) {
        this.$tap.alert('里程碑及任务的计划达成时间不能早于所属项目的计划开始时间！')
        this.$set(this.model, 'planfinishtime', null)
        return
      }
      if (!validateNull(this.maxTaskDate) && this.formatDateToMicro(time) < this.formatDateToMicro(this.maxTaskDate)) {
        this.$tap.alert('里程碑及任务的计划达成时间不能早于任务的计划达成时间！')
        this.$set(this.model, 'planfinishtime', null)
        return
      }
    },
    /** 格式化日期 **/
    formatDateToMicro(date) {
      return new Date(formatDate(new Date(date), 'yyyy-MM-dd')).getTime()
    },
    checkPlanUnfinish() {
      getAction(this.url.checkPlanUnfinish, { projectcode: validateNull(this.projectinfo.projectcode) ? this.model.projectcode : this.projectinfo.projectcode }).then(res => {
        if (res.success) {
            this.planUnfinish = res.result
        }
      })
    }
  }
}
</script>

